//
//  CYWinNumCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/19.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYWinNumCell.h"
#import "CYTrendModel.h"
#define MAP 10
@interface CYWinNumCell ()
@property (nonatomic, strong) CYLabel *dateLBL;//期号
@property (nonatomic, strong) CYLabel *rightLBL;//最右边的label
@property (nonatomic, strong) CYLabel *rightSecLBL;//右边第二个label
@property (nonatomic, strong) CYLabel *midLBL;//中间的label
@property (nonatomic, strong) CYView *line;//cell分割线
@property (nonatomic, assign) NSUInteger count;//上次label的个数
@end
@implementation CYWinNumCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.line.hidden = NO;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (CYLabel *)dateLBL{
    if (!_dateLBL) {
        _dateLBL = [[CYLabel alloc]initWithText:nil size:16 weight:UIFontWeightLight];
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(MAP);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}

- (CYLabel *)rightLBL{
    if (!_rightLBL) {
        _rightLBL = [[CYLabel alloc]initWithText:nil size:16 weight:UIFontWeightLight];
        [self addSubview:_rightLBL];
        
        [_rightLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).with.offset([CYTrendSource getRightLBLCenterX:_type]);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_rightLBL.bounds.size);
        }];
    }
    return _rightLBL;
}

- (CYLabel *)rightSecLBL{
    if (!_rightSecLBL) {
        _rightSecLBL = [[CYLabel alloc]initWithText:nil size:16 weight:UIFontWeightLight];
        [self addSubview:_rightSecLBL];
        
        [_rightSecLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).with.offset([CYTrendSource getRightSecLBLCenterX:_type]);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_rightSecLBL.bounds.size);
        }];
    }
    return _rightSecLBL;
}

- (CYLabel *)midLBL{
    if (!_midLBL) {
        _midLBL = [[CYLabel alloc]initWithText:nil size:16 weight:UIFontWeightLight];
        [self addSubview:_midLBL];
        
        [_midLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).with.offset([CYTrendSource getMidLBLCenterX:_type]);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_midLBL.bounds.size);
        }];
    }
    return _midLBL;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        [self addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
    }
    return _line;
}

- (void)setType:(TrendType)type{
    if (_type != type) {
        _type = type;
    }
}

- (void)setModel:(CYWinModel *)model{
    if (_model != model) {
        _model = model;
        [self setupDataLBLText];
        [self setupRightLBLText];
        [self setupRightSecLBLText];
        [self setupMidLBLText];
        [self setupNumbers];
    }
}

- (void)setupDataLBLText{
    self.dateLBL.text = [CYTrendSource getDateLBLText:_model.number type:_type];
    [_dateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_dateLBL.bounds.size);
    }];
}

- (void)setupRightLBLText{
    self.rightLBL.text = [CYTrendSource getRightLBLText:_model.data type:_type];
    self.rightLBL.textColor = TEXT_GRAY_COLOR;
    [_rightLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset([CYTrendSource getRightLBLCenterX:_type]);
        make.size.mas_equalTo(_rightLBL.bounds.size);
    }];
}

- (void)setupRightSecLBLText{
    self.rightSecLBL.text = [CYTrendSource getRightSecLBLText:_model.data type:_type];
    self.rightSecLBL.textColor = (_type == ShiShiCaiType || _type == KuaiSanType)?TEXT_GRAY_COLOR:BLUE_COLOR;
    [_rightSecLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset([CYTrendSource getRightSecLBLCenterX:_type]);
        make.size.mas_equalTo(_rightSecLBL.bounds.size);
    }];
}

- (void)setupMidLBLText{
    self.midLBL.text = [CYTrendSource getMidLBLText:_model.data type:_type];
    self.midLBL.textColor = (_type != KuaiSanType)?TEXT_GRAY_COLOR:BLUE_COLOR;
    [_midLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset([CYTrendSource getMidLBLCenterX:_type]);
        make.size.mas_equalTo(_midLBL.bounds.size);
    }];
}

- (void)setupNumbers{
    
    [self clearContent];
    NSArray *winNums = [_model.data componentsSeparatedByString:@","];
    CGFloat originX = [CYTrendSource getNumbersOriginX:_type];//第一个号码距离的originX
    CGFloat gap = [CYTrendSource getNumbersMap:_type];//号码之间的间距
    CGFloat num_width = [CYTrendSource getNumWidth:_type];//号码的宽度

    for (int i = 0; i < winNums.count; i ++) {
        CYLabel *label = [[CYLabel alloc]initWithText:winNums[i] size:16 textcolor:THEME_COLOR];
        label.tag = 200+i;
        [self addSubview:label];
        
        [label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(originX + (gap + num_width) *i);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(label.bounds.size);
        }];
    }
}

- (void)clearContent{
    
    for ( CYLabel *label in [self subviews]) {
        if (label.tag >= 200 && label.tag < 220) {
            [label removeFromSuperview];
        }
    }
}


/**/
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
